using System;
using UnityEngine;
using DG.Tweening;
using Locomotion;

namespace Screens
{
    public class OnScreen : MonoBehaviour
    {
        [SerializeField] private ScreenData screenData;
        
        
        private void OnEnable()
        {
            GroundDetector.AnyCharacterGroundedChanged += GroundDetector_AnyCharacterGroundedChanged;
        }

        private void OnDisable()
        {
            GroundDetector.AnyCharacterGroundedChanged -= GroundDetector_AnyCharacterGroundedChanged;
        }

        private void GroundDetector_AnyCharacterGroundedChanged(GroundDetector.GroundChangedEventArgs groundChangedEventArgs)
        {
            if (groundChangedEventArgs.GroundDetector.IsGrounded)
            {
                MoveDownwards();
            }
        }

        private void MoveDownwards()
        {
            transform.DOMoveY(
                transform.position.y - screenData.MoveDistanceY,
                screenData.MoveDuration);
        }
    }
}
